<?php 
class ModelPhoneCallType extends Model {
	public function addCallType($data) {
		$this->db->query("INSERT INTO phone_call_types SET name = '" . $this->db->escape($data['name']) . "', sort_order = '" . (int)$data['sort_order'] . "'");

		//$call_type_id = $this->db->getLastId();

// 		foreach ($data['phone_call_types_description'] as $language_id => $value) {
// 			$this->db->query("INSERT INTO phone_call_types_description SET call_type_id = '" . (int)$call_type_id . "', language_id = '" . (int)$language_id . "', name = '" . $this->db->escape($value['name']) . "'");
// 		}
	}

	public function editCallType($call_type_id, $data) {
		$this->db->query("UPDATE phone_call_types SET name = '" . $this->db->escape($data['name']) . "', sort_order = '" . (int)$data['sort_order'] . "' WHERE call_type_id = '" . (int)$call_type_id . "'");

// 		$this->db->query("DELETE FROM phone_call_types_description WHERE call_type_id = '" . (int)$call_type_id . "'");

// 		foreach ($data['phone_call_types_description'] as $language_id => $value) {
// 			$this->db->query("INSERT INTO phone_call_types_description SET call_type_id = '" . (int)$call_type_id . "', language_id = '" . (int)$language_id . "', name = '" . $this->db->escape($value['name']) . "'");
// 		}
	}

	public function deleteCallType($call_type_id) {
		$this->db->query("DELETE FROM phone_call_types WHERE call_type_id = '" . (int)$call_type_id . "'");
		//$this->db->query("DELETE FROM phone_call_types_description WHERE call_type_id = '" . (int)$call_type_id . "'");
	}

	public function getCallType($call_type_id) {
		$query = $this->db->query("SELECT * FROM phone_call_types WHERE call_type_id = '" . (int)$call_type_id . "'");

		return $query->row;
	}

	public function getCallTypes($data = array()) {
		$sql = "SELECT * FROM phone_call_types pct";

		$sort_data = array(
				'pct.name',
				'pct.sort_order'
		);
		
		if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
			$sql .= " ORDER BY " . $data['sort'];	
		} else {
			$sql .= " ORDER BY pct.name";	
		}	

		if (isset($data['order']) && ($data['order'] == 'DESC')) {
			$sql .= " DESC";
		} else {
			$sql .= " ASC";
		}

		if (isset($data['start']) || isset($data['limit'])) {
			if ($data['start'] < 0) {
				$data['start'] = 0;
			}				

			if ($data['limit'] < 1) {
				$data['limit'] = 20;
			}	

			$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
		}	

		$query = $this->db->query($sql);

		return $query->rows;
	}

	public function getCallTypeDescriptions($call_type_id) {
		$phone_call_types_data = array();

		$query = $this->db->query("SELECT * FROM phone_call_types_description WHERE call_type_id = '" . (int)$call_type_id . "'");

		foreach ($query->rows as $result) {
			$phone_call_types_data[$result['language_id']] = array('name' => $result['name']);
		}

		return $phone_call_types_data;
	}

	public function getTotalCallTypes() {
		$query = $this->db->query("SELECT COUNT(*) AS total FROM phone_call_types");

		return $query->row['total'];
	}	
}
?>